package com.xxx.jdbc;

import org.junit.Test;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/28 14:27
 * @Copyright 博为峰
 */
public class JdbcDemo02 {
    //jdbc4版本
    public static void main(String[] args) {
//        m1();
        query();
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(100, "岳云鹏", 36, 170.36, "男", new Date(System.currentTimeMillis()), 2));
        list.add(new Student(100, "孙越", 26, 176.36, "男", new Date(System.currentTimeMillis()), 2));
        insert(list);

        /*
           1.插入集合中的所有数据
           2.实现更新删除操作
         */
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/j13", "root", "123456");
    }

    private static void insert(ArrayList<Student> list) {
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
        ) {
            String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values ('赵四',60,180.96,'男','2021-07-28 16:01:26',2)";
            int i = statement.executeUpdate(sql);
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void query() {
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("select * from stu");) {
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int anInt = resultSet.getInt(1);
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                double height = resultSet.getDouble("height");
                String sex = resultSet.getString("sex");
                Date birthday = resultSet.getDate("birthday");
                int claId = resultSet.getInt("cla_id");

                Student student = new Student(anInt, name, age, height, sex, birthday, claId);
                list.add(student);
            }
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void m1() throws SQLException {
        //加载驱动
//        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/j13", "root", "123456");
        //获取执行静态sql语句的Statement对象
        Statement statement = connection.createStatement();
        //执行sql语句
        ResultSet resultSet = statement.executeQuery("select * from stu");
        while (resultSet.next()) {
            int anInt = resultSet.getInt(1);
            System.out.println(anInt);
            String name = resultSet.getString("name");
            System.out.println(name);
        }
    }

}
